var users;
var userMap = {};//用户信息的主键和用户信息的映射，为修改信息快速查询用户信息使用
$.ajax({
    type: 'get',
    url: "/user/list/all",
    contentType: 'application/json;charset=utf-8',
    success: function(data){    //请求成功时的回调函数
        if(data.code == "1"){
            users = eval(data.data);
            createTable(users);
        }else{
            alert(data.msg + data.data);
        }
    },
});

function createTable(users) {
    var tbody = $("[sid='user-list']");
    for(var i = 0;i < users.length;i++){
        var user = users[i];
        userMap[user.id] = user;
        var tr = $("<tr id='user-"+user.id+"'></tr>");

        var th = $("<th scope='row'></th>");
        $("<div>"+(i+1)+"</div>").appendTo(th);
        th.appendTo(tr);


        createTd(user.loginId).appendTo(tr);
        createTd(user.pass).appendTo(tr);
        createTd(user.name).appendTo(tr);
        createTd(user.email).appendTo(tr);
        createTd(user.phone).appendTo(tr);
        createTd(user.roleInfo.roleName == "admin" ? "管理员" : "超级管理员").appendTo(tr);
        createTd(user.isActive == 0 ? "未激活" : "已激活").attr("sid","status-" + user.id).appendTo(tr);

        var td = $("<td></td>");

        $("<button class='btn btn-default btn-rounded' type='button' data-toggle='modal' data-target='#myModal'></button>").html($("#temp-note").html())
            .attr("onclick","update('"+user.id+"')").appendTo(td);

        //冻结和激活按钮
        var keyBtn = $("<button></button>").attr("class", "btn btn-default btn-rounded").attr("type", "button").html($("#temp-key").html())
            .attr("onclick", "active('" + user.id + "')").attr("sid","user-key-"+user.id);
        var lockBtn = $("<button></button>").attr("class", "btn btn-default btn-rounded").attr("type", "button").html($("#temp-lock").html())
            .attr("onclick", "freeze('" + user.id + "')").attr("sid","user-lock-"+user.id);

        if(user.isActive){
            keyBtn.hide();
        }else{
            lockBtn.hide();
        }

        keyBtn.appendTo(td);
        lockBtn.appendTo(td);

        $("<button></button>").attr("class","btn btn-default btn-rounded").attr("type","button")
            .attr("onclick","deleteUser('"+user.id+"')").html($("#temp-trash").html()).appendTo(td);

        td.appendTo(tr);

        tr.appendTo(tbody);
    }
}

function createTd(data) {

    var td = $("<td></td>");
    $("<div>"+data+"</div>").appendTo(td);
    return td;
}

function update(userId) {

    var user = userMap[userId];
    $("#m-userId").val(user.id);
    $("#m-loginId").val(user.loginId);
    $("#m-pass").val(user.pass);
    $("#m-name").val(user.name);
    $("#m-email").val(user.email);
    $("#m-phone").val(user.phone);
    $("#m-role").find("option[value='"+user.roleInfo.id+"']").attr("selected",true);
}

function submitUpdate() {
    var updUser = {};
    var roleInfo = {};

    updUser.id = $("#m-userId").val();
    updUser.loginId = $("#m-loginId").val();
    updUser.pass = $("#m-pass").val();
    updUser.name = $("#m-name").val();
    updUser.email = $("#m-email").val();
    updUser.phone = $("#m-phone").val();
    roleInfo.id = $("#m-role").find("option:selected").val();
    roleInfo.roleName = roleInfo.id == 1 ? "root" : "admin";
    updUser.roleInfo = roleInfo;

    $.ajax({
        type: 'post',
        url: "/user/update",
        contentType: 'application/json;charset=utf-8',
        data:JSON.stringify(updUser),
        success: function(data){    //请求成功时的回调函数
            if(data.code == "1"){
                alert("更新成功");
                window.location.href = "/user/management";
            }else{
                alert(data.msg);
            }
        },
    });
}

function active(userId) {

    $.ajax({
        type: 'get',
        url: "/user/active/" + userId,
        contentType: 'application/json;charset=utf-8',
        success: function(data){    //请求成功时的回调函数
            if(data.code == "1"){
                alert("激活成功");
                $("[sid='user-lock-"+userId+"']").show();
                $("[sid='user-key-"+userId+"']").hide();
                $("[sid='status-"+userId+"']").html("<div>已激活</div>");
            }else{
                alert(data.msg);
            }
        },
    });
}

function freeze(userId) {
    $.ajax({
        type: 'get',
        url: "/user/freeze/" + userId,
        contentType: 'application/json;charset=utf-8',
        success: function(data){    //请求成功时的回调函数
            if(data.code == "1"){
                alert("锁定成功");
                $("[sid='user-lock-"+userId+"']").hide();
                $("[sid='user-key-"+userId+"']").show();
                $("[sid='status-"+userId+"']").html("<div>未激活</div>");
            }else{
                alert(data.msg);
            }
        },
    });
}

function deleteUser(userId){

    $.ajax({
        type: 'get',
        url: "/user/delete/" + userId,
        contentType: 'application/json;charset=utf-8',
        success: function(data){    //请求成功时的回调函数
            if(data.code == "1"){
                alert("删除成功");
                window.location.href = "/user/management";
            }else{
                alert(data.msg);
            }
        },
    });
}